function tianTianWaiLian(links_id,redirect) {
    $.ajax({
        "url":"https://bab.moreqifu.cn/jslinks_detial",
        "data":{"links_id":links_id},
        success:function(result){
            console.log(result)
            var mobile_flag = isMobile();
            var weixin = isWeiXin();
            if(mobile_flag){
                var weibo = isWeibo();
                if (weixin) {
                    if (result.data.platfrom != 1) {
                        $("#wailian_web_jump").hide();
                        $("#wechatbtn").show();
                        getJsLinksMiniapp(links_id)
                    } else {
                        window.location.href = result.data.url;
                    }
                } else if (weibo) {
                    $("#wailian_web_jump").show();
                    $("#wechatbtn").hide();
                    $("#wailian_web_jump").attr("href","sinaweibo://wbdiversion?username="+result.data.username+"&path="+result.data.path);
                    if (redirect) {
                        window.location.href="sinaweibo://wbdiversion?username="+result.data.username+"&path="+result.data.path;
                    }
                } else {
                    $("#wailian_web_jump").show();
                    $("#wechatbtn").hide();
                    $("#wailian_web_jump").attr("href",result.data.urlscheme);
                    if (redirect) {
                        window.location.href= result.data.urlscheme;
                    }
                }
            } else {
                $("#wailian_web_jump").show();
                $("#wechatbtn").hide();
                window.location.href="https://bab.moreqifu.cn/l/"+links_id;
            }
        },
        error: function() {
            toast("接口异常,请稍后重试！")
        }
    });
}

function getJsLinksMiniapp(links_id) {
    $.ajax({
        "url":"https://bab.moreqifu.cn/jslinks_miniapp",
        "data":{
            "links_id":links_id,
            "url":encodeURI(window.location.href)
        },
        "type":"GET",
        success:function(result){
            if (result.status == "success") {
                $("#wechatbtn").attr({"username":result.data.username,"path":result.data.path});
                wechatMiniapp(result.data.appid,result.data.timestamp,result.data.nonceStr,result.data.signature)
            } else {
                toast(result.msg);
            }
        },
        error: function() {
            toast("接口异常,请稍后重试！")
        }
    });
}

function wechatMiniapp(appid,timestamp,nonceStr,signature) {
    wx.config({
        debug: false,
        appId: appid,
        timestamp: timestamp, 
        nonceStr: nonceStr, 
        signature: signature,
        jsApiList: ["chooseImage"],
        openTagList: ['wx-open-launch-weapp']
    });
    wx.ready(function () {
        console.log("ready");
    });

    wx.error(function (res) {
        console.log("res", res);
    });        

    var btn = document.getElementById('wechatbtn');
    btn.addEventListener('launch', function (e) {
        console.log('success');
    });
    btn.addEventListener('error', function (e) {
        console.log('fail', e.detail);
    });
}

function isWeiXin(){
    var ua = navigator.userAgent.toLowerCase()
    var isWXWork = ua.match(/wxwork/i) == 'wxwork'
    var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
    return isWeixin;
}

function isWeibo() {
    var ua = navigator.userAgent;
    if(ua.indexOf('Weibo') > -1) {
        return true;
    } else {
        return false;
    }
}

function isMobile() {
    if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|XiaoMi|IEMobile)/i)) {
        return true;
    } else {
        return false;
    }
}

function toast(message,options) {
    var durations = {
        'long': 3500,
        'short': 2000
    };

    //计算显示时间
     options = $.extend({
        duration: 'short'
    }, options || {});

    var CLASS_ACTIVE = 'mui-active';
    if (typeof options.duration === 'number') {
        duration = options.duration>0 ? options.duration:durations['short'];
    } else {
        duration = durations[options.duration];
    }
    if (!duration) {
        duration = durations['short'];
    }
    var toast = document.createElement('div');
    toast.classList.add('mui-toast-container');
    toast.innerHTML = '<div class="' + 'mui-toast-message' + '">' + message + '</div>';
    toast.addEventListener('webkitTransitionEnd', function() {
        if (!toast.classList.contains(CLASS_ACTIVE)) {
            toast.parentNode.removeChild(toast);
            toast = null;
        }
    });
    //点击则自动消失
    toast.addEventListener('click', function() {
        toast.parentNode.removeChild(toast);
        toast = null;
    });
    document.body.appendChild(toast);
    toast.offsetHeight;
    toast.classList.add(CLASS_ACTIVE);
    setTimeout(function() {
        toast && toast.classList.remove(CLASS_ACTIVE);
    }, duration);
    
    return {
        isVisible: function() {return !!toast;}
    }
}
